package com.ruoyi.hotel.util;

import com.ruoyi.common.utils.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    public static Boolean isBelong(){

        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        Date now =null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse("00:00");
            endTime = df.parse("06:00");
        } catch (Exception e) {
            e.printStackTrace();
        }

        Boolean flag = belongCalendar(now, beginTime, endTime);
        return flag;
    }


    /**
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取日期字符串
     * @return
     */
    public static String getDateByNumStr(int num){
        Date date=new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //把日期往后增加一天.整数往后推,负数往前移动(1:表示明天、-1：表示昨天，0：表示今天)
        calendar.add(Calendar.DATE,num);

        //这个时间就是日期往后推一天的结果
        date=calendar.getTime();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String tomorrowStr = formatter.format(date);
        return tomorrowStr;
    }

    /**
     * 获取日期字符串
     * @return
     */
    public static String getDateByNumStr(String beginDate,int num){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date beginTime = null;
        Date date = null;

        try {
            beginTime = formatter.parse(beginDate);
        } catch (Exception e) {
            e.printStackTrace();
        }


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginTime);
        //把日期往后增加一天.整数往后推,负数往前移动(1:表示明天、-1：表示昨天，0：表示今天)
        calendar.add(Calendar.DATE,num);

        //这个时间就是日期往后推一天的结果
        date=calendar.getTime();
        String tomorrowStr = formatter.format(date);
        return tomorrowStr;
    }


    /**
     * 获得两个日期相差的天数
     * @param beginDate
     * @param endDate
     */


    public static int differentDays(String beginDate,String endDate){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate1 = LocalDate.parse(beginDate, formatter);
        LocalDate localDate2 = LocalDate.parse(endDate, formatter);
        Period period = Period.between(localDate1, localDate2);
        int days = period.getDays();
        return days;
    }

    public static void main(String[] args) {

        String date1 = "2024-03-20";
        String date2 = "2024-03-25";


        int days = differentDays(date1,date2);
        String ss = getDateByNumStr(date1,0-days-1);
        String ss2 = getDateByNumStr(date2,0-days-1);
        System.out.println(ss);
        System.out.println(ss2);
        System.out.println(days);

    }
}
